package method

import (
	"gitee.com/wanjimao/common-util-yun/actiontiktok/cases/actiontiktokcasemysql"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"strings"
	"time"
)

const (
	TimeWeekSundayString    = "Sunday"    //星期日
	TimeWeekMondayString    = "Monday"    //星期1
	TimeWeekTuesdayString   = "Tuesday"   //星期2
	TimeWeekWednesdayString = "Wednesday" //星期3
	TimeWeekThursdayString  = "Thursday"  //星期4
	TimeWeekFridayString    = "Friday"    //星期5
	TimeWeekSaturdayString  = "Saturday"  //星期6
)

func GetFirstDateOfWeek(now time.Time) (weekMonday time.Time) {
	offset := int(time.Monday - now.Weekday())
	if offset > 0 {
		offset = -6
	}
	weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
	return weekStartDate
}

func GetWeekNumberSpecial(tmp time.Time) int {
	data := map[string]int{
		TimeWeekSundayString:    7,
		TimeWeekMondayString:    1,
		TimeWeekTuesdayString:   2,
		TimeWeekWednesdayString: 3,
		TimeWeekThursdayString:  4,
		TimeWeekFridayString:    5,
		TimeWeekSaturdayString:  6,
	}
	return data[tmp.Weekday().String()]
}

// GetLuckValueWeekStartAndEndJoin     幸运值榜单 结榜
func GetLuckValueWeekStartAndEndJoin(nowTime time.Time) string {
	firstDay := GetFirstDateOfWeek(nowTime)                        //获取周一
	LastTwoDay := firstDay.AddDate(0, 0, -3).Format(time.DateOnly) //上周六
	thisOneDay := firstDay.AddDate(0, 0, 3).Format(time.DateOnly)  // 这周周5
	thisTwoDay := firstDay.AddDate(0, 0, 4).Format(time.DateOnly)  // 这周六
	nextOneDay := firstDay.AddDate(0, 0, 10).Format(time.DateOnly) // 下周5
	specialNumber := GetWeekNumberSpecial(nowTime)
	//如果当前是大于等于周二，算到下一轮，否则上一轮
	if specialNumber >= 5 {
		return thisTwoDay + "加" + nextOneDay
	} else {
		return LastTwoDay + "加" + thisOneDay
	}
}

// GetNowGiftWeekStartAndEndJoin 女神榜单   周二 到 周一 结榜
func GetNowGiftWeekStartAndEndJoin(nowTime time.Time) string {
	//nowTime := time.Now()
	//获取当前时间,动态匹配key
	firstDay := GetFirstDateOfWeek(nowTime)                        //获取周一
	LastTwoDay := firstDay.AddDate(0, 0, -7).Format(time.DateOnly) //上周六
	thisOneDay := firstDay.AddDate(0, 0, -1).Format(time.DateOnly) // 这周周5
	thisTwoDay := firstDay.AddDate(0, 0, 0).Format(time.DateOnly)  // 这周六
	nextOneDay := firstDay.AddDate(0, 0, 6).Format(time.DateOnly)  // 下周5
	specialNumber := GetWeekNumberSpecial(nowTime)
	//如果当前是大于等于周二，算到下一轮，否则上一轮
	if specialNumber >= 1 {
		return thisTwoDay + "加" + nextOneDay
	} else {
		return LastTwoDay + "加" + thisOneDay
	}
}

// GetNowBeautyContestWeekStartAndEndJoin 女神榜单   周四 到 周三结榜
func GetNowBeautyContestWeekStartAndEndJoin(nowTime time.Time) string {
	//nowTime := time.Now()
	//获取当前时间,动态匹配key
	firstDay := GetFirstDateOfWeek(nowTime)                        //获取周一
	LastTwoDay := firstDay.AddDate(0, 0, -4).Format(time.DateOnly) //上周六
	thisOneDay := firstDay.AddDate(0, 0, 2).Format(time.DateOnly)  // 这周周5
	thisTwoDay := firstDay.AddDate(0, 0, 3).Format(time.DateOnly)  // 这周六
	nextOneDay := firstDay.AddDate(0, 0, 9).Format(time.DateOnly)  // 下周5
	specialNumber := GetWeekNumberSpecial(nowTime)
	//如果当前是大于等于周二，算到下一轮，否则上一轮
	if specialNumber >= 4 {
		return thisTwoDay + "加" + nextOneDay
	} else {
		return LastTwoDay + "加" + thisOneDay
	}
}

func GetNowWeekStartAndEnd(nowTime time.Time) (string, string) {
	//nowTime := time.Now()
	//获取当前时间,动态匹配key
	firstDay := GetFirstDateOfWeek(nowTime) //获取周一
	//LastLastTwoDay := firstDay.AddDate(0, 0, -13).Format(time.DateOnly) //上次轮训的开始周二
	//LastOneDay := firstDay.AddDate(0, 0, -7).Format(time.DateOnly)      //这次轮询的周一
	LastTwoDay := firstDay.AddDate(0, 0, -2).Format(time.DateOnly) //上周六
	thisOneDay := firstDay.AddDate(0, 0, 4).Format(time.DateOnly)  // 这周周5
	thisTwoDay := firstDay.AddDate(0, 0, 5).Format(time.DateOnly)  // 这周六
	nextOneDay := firstDay.AddDate(0, 0, 11).Format(time.DateOnly) // 下周5
	specialNumber := GetWeekNumberSpecial(nowTime)
	//如果当前是大于等于周二，算到下一轮，否则上一轮
	if specialNumber >= 6 {
		return thisTwoDay, nextOneDay
	} else {
		return LastTwoDay, thisOneDay
	}
}

func GetNowWeekStartAndEndJoin(nowTime time.Time) string {
	//nowTime := time.Now()
	//获取当前时间,动态匹配key
	firstDay := GetFirstDateOfWeek(nowTime) //获取周一
	////LastLastTwoDay := firstDay.AddDate(0, 0, -13).Format(time.DateOnly) //上次轮训的开始周二
	////LastOneDay := firstDay.AddDate(0, 0, -7).Format(time.DateOnly)      //这次轮询的周一
	//LastTwoDay := firstDay.AddDate(0, 0, -6).Format(time.DateOnly) //这期周二
	//thisOneDay := firstDay.AddDate(0, 0, 0).Format(time.DateOnly)  // 这周一
	//thisTwoDay := firstDay.AddDate(0, 0, 1).Format(time.DateOnly)  // 下周2
	//nextOneDay := firstDay.AddDate(0, 0, 7).Format(time.DateOnly)  // 下周一

	LastTwoDay := firstDay.AddDate(0, 0, -2).Format(time.DateOnly) //上周六
	thisOneDay := firstDay.AddDate(0, 0, 4).Format(time.DateOnly)  // 这周周5
	thisTwoDay := firstDay.AddDate(0, 0, 5).Format(time.DateOnly)  // 这周六
	nextOneDay := firstDay.AddDate(0, 0, 11).Format(time.DateOnly) // 下周5
	specialNumber := GetWeekNumberSpecial(nowTime)
	//如果当前是大于等于周二，算到下一轮，否则上一轮
	if specialNumber >= 6 {
		return thisTwoDay + "加" + nextOneDay
	} else {
		return LastTwoDay + "加" + thisOneDay
	}
}

func GetLastWeekStartAndEnd(nowTime time.Time) (string, string) {
	//nowTime := time.Now()
	//获取当前时间,动态匹配key
	firstDay := GetFirstDateOfWeek(nowTime)                            //获取周一
	LastLastTwoDay := firstDay.AddDate(0, 0, -9).Format(time.DateOnly) //上次轮训的开始周二
	LastOneDay := firstDay.AddDate(0, 0, -3).Format(time.DateOnly)     //这次轮询的周一
	LastTwoDay := firstDay.AddDate(0, 0, -2).Format(time.DateOnly)     //上周六
	thisOneDay := firstDay.AddDate(0, 0, 4).Format(time.DateOnly)      // 这周5
	//thisTwoDay := firstDay.AddDate(0, 0, 1).Format(time.DateOnly)       // 下周2
	//nextOneDay := firstDay.AddDate(0, 0, 7).Format(time.DateOnly)       // 下周一
	specialNumber := GetWeekNumberSpecial(nowTime)
	//如果当前是大于等于周2，算到下一轮，否则上一轮
	if specialNumber >= 6 {
		return LastTwoDay, thisOneDay
	} else {
		return LastLastTwoDay, LastOneDay
	}
}

func JoinCpUser(userId, userIdOther string) string {
	//比较大小，对用户id排序
	if compare := strings.Compare(userId, userIdOther); compare == -1 {
	} else {
		//如果 UserId > UserIdOther  ,互换位置
		maxUid := userId
		userId = userIdOther
		userIdOther = maxUid
	}
	return userId + "加" + userIdOther
}

func SortUserId(userId, userIdOther string) (string, string) {
	//比较大小，对用户id排序
	if compare := strings.Compare(userId, userIdOther); compare == -1 {
	} else {
		//如果 UserId > UserIdOther  ,互换位置
		maxUid := userId
		userId = userIdOther
		userIdOther = maxUid
	}
	return userId, userIdOther
}

func UniqueId() string {
	return primitive.NewObjectID().Hex()
}

func GetWeekNumber(tmp time.Time) int {
	data := map[string]int{
		TimeWeekSundayString:    0,
		TimeWeekMondayString:    1,
		TimeWeekTuesdayString:   2,
		TimeWeekWednesdayString: 3,
		TimeWeekThursdayString:  4,
		TimeWeekFridayString:    5,
		TimeWeekSaturdayString:  6,
	}
	return data[tmp.Weekday().String()]
}

func ElementIsTimeType(eleType int64) bool {
	return eleType == actiontiktokcasemysql.ActionElementCasesClothing || eleType == actiontiktokcasemysql.ActionElementCasesAppearance || eleType == actiontiktokcasemysql.ActionElementCasesToy
}

func ElementIsNumberType(eleType int64) bool {
	return eleType == actiontiktokcasemysql.ActionElementCasesTool
}
